{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "252d59fb-460d-4608-be1d-961f407fae6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[59 68 80]\n",
      " [77 73 71]\n",
      " [79 99 88]\n",
      " [97 82 70]\n",
      " [63 75 97]\n",
      " [56 45 56]\n",
      " [42 87 50]\n",
      " [59 49 88]\n",
      " [71 87 68]\n",
      " [41 58 99]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.randint(40,100,(10,3))\n",
    "print(arr)\n",
    "np.savetxt('test.csv',arr,fmt='%d',header='语文,数学,物理',delimiter=\",\",comments='',encoding='utf-8-sig')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "826cc6e6-467d-4ea2-9a75-854af447b0e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "arr = np.loadtxt('test.csv',delimiter=',',skiprows=1,dtype=int,encoding='utf-8-sig')\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9260dce2-094b-450a-82ec-95c9b2d06064",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[59 68 80]\n",
      " [77 73 71]\n",
      " [79 99 88]\n",
      " [97 82 70]\n",
      " [63 75 97]\n",
      " [56 45 56]\n",
      " [42 87 50]\n",
      " [59 49 88]\n",
      " [71 87 68]\n",
      " [41 58 99]]\n",
      "****************************************************************************************************\n",
      "各科均值: [64.4 72.3 76.7]\n",
      "各科标准差： [16.30460058 16.60752841 15.71655178]\n",
      "各科最高分： [97 99 99]\n",
      "各科最低分： [41 45 50]\n",
      "各科极差： [56 54 49]\n",
      "全班均值： 71.13333333333334\n",
      "总体标准差： 16.993593564111805\n",
      "50%的百分比 61.0\n",
      "四分位数（语文）： [56.75 61.   75.5 ]\n",
      "[[56.75 60.5  68.5 ]\n",
      " [61.   74.   75.5 ]\n",
      " [75.5  85.75 88.  ]]\n",
      "61.0\n",
      "[61.  74.  75.5]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "S = np.loadtxt('test.csv',delimiter=',',skiprows=1,dtype=int,encoding='utf-8-sig')\n",
    "print(S)\n",
    "print('*'*100)\n",
    "print('各科均值:',np.mean(S,axis=0))\n",
    "print(\"各科标准差：\", np.std(S, axis=0))\n",
    "print(\"各科最高分：\", np.max(S, axis=0))\n",
    "print(\"各科最低分：\", np.min(S, axis=0))\n",
    "#最大值与最小值之间的差距\n",
    "print(\"各科极差：\", np.ptp(S, axis=0))\n",
    "#总体统计\n",
    "print(\"全班均值：\", np.mean(S))\n",
    "print(\"总体标准差：\", np.std(S))\n",
    "print(\"50%的百分比\",np.percentile(S[:,0],50))#语文中有62%的人分数低于50\n",
    "print(\"四分位数（语文）：\", np.percentile(S[:,0], [25,50,75]))\n",
    "#按照科类统计\n",
    "print(np.percentile(S,[25,50,75],axis=0))\n",
    "#统计每一科的中位数，不受极端值影响，更能代表“典型水平”\n",
    "print(np.median(S[:,0]))\n",
    "print(np.median(S,axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "60fe8666-c5e8-41e3-8dda-cff62b96b99e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1.0\n",
      "1    2.0\n",
      "2    3.0\n",
      "3    4.0\n",
      "4    5.0\n",
      "5    6.0\n",
      "6    NaN\n",
      "dtype: float64\n",
      "3.0\n",
      "a    1.0\n",
      "b    2.0\n",
      "c    3.0\n",
      "d    4.0\n",
      "e    5.0\n",
      "f    6.0\n",
      "g    NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "lst = [1,2,3,4,5,6,None]\n",
    "S = pd.Series(lst)\n",
    "print(S)\n",
    "print(S[2])\n",
    "#创建一行\n",
    "S1 = pd.Series(lst,index=list('abcdefg'),dtype=float)\n",
    "print(S1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2abdcf1a-f9cd-4d43-b462-b743d47d8412",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       [1, 2, 3, 4]\n",
      "1       [5, 6, 7, 8]\n",
      "2    [9, 10, 11, 12]\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "arr = np.array([\n",
    "    [1,2,3,4],\n",
    "    [5,6,7,8],\n",
    "    [9,10,11,12]\n",
    "])\n",
    "#会报错，Series表示的是一行\n",
    "#S = pd.Series(arr)\n",
    "#此时其实是每一行当做一个值\n",
    "S = pd.Series(list(arr))\n",
    "print(S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "42e6d40e-de05-494f-b0b2-9c2697891a30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7] <class 'numpy.ndarray'>\n",
      "0     1\n",
      "1     2\n",
      "2    10\n",
      "3     4\n",
      "4     5\n",
      "5     6\n",
      "6     7\n",
      "dtype: int64\n",
      "[ 1  2 10  4  5  6  7]\n",
      "[99  2 10  4  5  6  7] [99  2 10  4  5  6  7]\n",
      "0    99\n",
      "1     2\n",
      "2    10\n",
      "3     4\n",
      "4     5\n",
      "5     6\n",
      "6     7\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "arr = np.array([1,2,3,4,5,6,7])\n",
    "s = pd.Series(arr)\n",
    "print(s.values,type(s.values))\n",
    "arr[2] = 10\n",
    "#修改了np，也会同时修改pandas\n",
    "print(s)\n",
    "#讲Series转换为numpy\n",
    "arr2 = s.to_numpy()\n",
    "#需要注意arr2就是原来numpy的指针，是一个对象\n",
    "print(arr2)\n",
    "arr2[0] = 99\n",
    "print(arr2,arr)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0305176a-2ce7-4375-a0d2-645c7a6cabac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "B    2\n",
      "D    4\n",
      "dtype: int64\n",
      "1\n",
      "A    1\n",
      "B    2\n",
      "C    3\n",
      "D    4\n",
      "dtype: int64\n",
      "E    5\n",
      "D    4\n",
      "C    3\n",
      "B    2\n",
      "A    1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "arr = np.array([1,2,3,4,5])\n",
    "S = pd.Series(arr,index=list('ABCDE'))\n",
    "#获取第一个元素\n",
    "print(S.iloc[0])\n",
    "#也可以进行切片操作\n",
    "print(S.iloc[1::2])\n",
    "#使用loc可以通过索引来访问\n",
    "print(S.loc['A'])\n",
    "#loc是包头包尾\n",
    "print(S.loc['A':'D'])\n",
    "print(S.loc['E'::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "83b31853-7e0c-4332-8821-710e78638980",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['A', 'B', 'C', 'D', 'E'], dtype='object')\n",
      "[80. 85. 90. nan 70.]\n",
      "float64\n",
      "(5,)\n",
      "None\n",
      "80.0\n",
      "80.0\n",
      "B    85.0\n",
      "C    90.0\n",
      "dtype: float64\n",
      "B    85.0\n",
      "C    90.0\n",
      "dtype: float64\n",
      "A    85.0\n",
      "B    90.0\n",
      "C    95.0\n",
      "D     NaN\n",
      "E    75.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "s = pd.Series([80, 85, 90, np.nan, 70], index=['A','B','C','D','E'])\n",
    "\n",
    "print(s.index)      # 索引\n",
    "print(s.values)     # 值（NumPy 数组）\n",
    "print(s.dtype)      # 数据类型\n",
    "print(s.shape)      # 长度\n",
    "print(s.name)       # 名称\n",
    "\n",
    "print(s['A'])\n",
    "#不能使用s[0]来访问\n",
    "# print(s[0])\n",
    "print(s.iloc[0])\n",
    "#可以使用索引\n",
    "print(s[s>80])\n",
    "print(s[(s>80)&(s<=90)])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4d8bdf7a-130d-43f8-9518-29d018f68351",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A     80.0\n",
      "B    105.0\n",
      "C    110.0\n",
      "D      NaN\n",
      "E     70.0\n",
      "dtype: float64\n",
      "91.25 110.0 70.0 <bound method Series.std of A     80.0\n",
      "B    105.0\n",
      "C    110.0\n",
      "D      NaN\n",
      "E     70.0\n",
      "dtype: float64>\n",
      "count      4.00000\n",
      "mean      91.25000\n",
      "std       19.31105\n",
      "min       70.00000\n",
      "25%       77.50000\n",
      "50%       92.50000\n",
      "75%      106.25000\n",
      "max      110.00000\n",
      "dtype: float64\n",
      "A    False\n",
      "B    False\n",
      "C    False\n",
      "D     True\n",
      "E    False\n",
      "dtype: bool\n",
      "A     80.00\n",
      "B    105.00\n",
      "C    110.00\n",
      "D     91.25\n",
      "E     70.00\n",
      "dtype: float64\n",
      "A     80.0\n",
      "B    105.0\n",
      "C    110.0\n",
      "E     70.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "s = pd.Series([80, 85, 90, np.nan, 70], index=['A','B','C','D','E'])\n",
    "#同样支持广播操作\n",
    "s[s>80]+=20\n",
    "print(s)\n",
    "#也有具体的方法\n",
    "print(s.mean(),s.max(),s.min(),s.std)\n",
    "#统计所有的数据\n",
    "print(s.describe())\n",
    "print(s.isna())              # 判断缺失\n",
    "print(s.fillna(s.mean()))    # 用均值填充缺失值\n",
    "print(s.dropna())            # 删除缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bb29f98-c1e8-4d69-b5a2-9dd6b7e004d4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
